import '../../../assets/js/libs/jquery.ztree.all.min.js'

export default {
  props:{
    isNeedChannelTree: [String, Boolean],
    isCheckbox: {
      type: Boolean,
      default: false
    },
    paramNameForChannelTree: {
      type: String,
      default: 'enterChannelNames'
    },
    selectedChannelNode:{
      type: String,
      default: ''
    },
    channelTreeData:Array,
    reloadChannelTree:{
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      noData:true,
      curPagePath:''
    }
  },
  mounted() {
    let userLoginId = window.sessionStorage.getItem('loginid')
    this.curPagePath = userLoginId+'-'+this.$route.path
    let channelTreeData = this.adjustChannelTreeData2()
    this.initChannelTree('channelTableTree',channelTreeData)
  },
  methods: {
    // 初始化区域树
    initChannelTree(treeId,channelTreeData){
      let self = this
      $.fn.zTree.destroy(treeId);
      if(!channelTreeData.length && !self.channelTreeData.length){
        this.noData = true
        return
      }
      let settings = {
        data: {
          simpleData: {
            enable: true
          }
        },
        check: {
          enable: true,
        },
        view: {
          showIcon: false,
          showLine: false,
        },
        callback: {
          
        }
      };
      let treeObj = null
      if(channelTreeData && channelTreeData.length>0){
        this.noData = false
        treeObj= $.fn.zTree.init($("#"+treeId), settings, channelTreeData);
      }else{
        treeObj = $.fn.zTree.init($("#"+treeId), settings, self.channelTreeData);
      }
      treeObj.expandAll(true);
      let allNodes = treeObj.getNodes()
      for(let i = 0; i<allNodes.length; i++){
        let nodeChildrenData = allNodes[i].children && allNodes[i].children.length ? allNodes[i].children : []
        for(let j = nodeChildrenData.length-1; j>=0; j--){
          if(nodeChildrenData[j].isHidden){
            treeObj.removeNode(nodeChildrenData[j]);
          }
        }
      }
    },
    OnCheckNode(event, treeId, treeNode) {
     
    },
    setSelectedChannelNode(channelArr){
      this.$emit('setSelectedChannelNode', channelArr);
    },
    adjustChannelTreeData2(){
      let curPageCache = window.localStorage.getItem(this.curPagePath)
      let _curPageCache = curPageCache ? JSON.parse(curPageCache) : {}
      let channelTreeData = _curPageCache.channelTreeData ? JSON.parse(_curPageCache.channelTreeData) : []
      let ids = _curPageCache.channelIds ? _curPageCache.channelIds : ''
      let pids = _curPageCache.channelPids ? _curPageCache.channelPids : '' //区域id
      for(let i = 0; i<channelTreeData.length; i++){
        let itemObj = channelTreeData[i] //停车场
        let dataList = itemObj.children && itemObj.children.length ? itemObj.children : []
        for(let j = 0; j<dataList.length; j++){
          let tmpObj = dataList[j] //区域
          let areaChannelData = tmpObj.children && tmpObj.children.length ? tmpObj.children : [] //区域通道数据
          for(let k = 0; k<areaChannelData.length; k++){
            let obj = areaChannelData[k]
            if(ids.indexOf(obj.channelId)>-1 && pids.indexOf(tmpObj.id)>-1){
              obj.checked = true
              continue;
            }
            obj.checked = false
          }
        }
      }
      return channelTreeData;
    },
  },
  watch: {
    'reloadChannelTree':function(){
      let channelTreeData = this.adjustChannelTreeData2()
      this.initChannelTree('channelTableTree',channelTreeData)
    },
  },
}